<? 
$pagename=LA_UPLOAD_VIDEO;
if (!$admin_area)
	include_once("header_inc.php");


if (!$admin_area)
{
	check_valid_user("");
	include_once("member_header.php");	
}
$video_ad_id=getParamInt("video_ad_id","");
$mainType=getPaqueteMainTypeFromAdId($video_ad_id);

//if (!$video_ad_id)
	//criticalError(LA_ERROR,LA_ERROR_NO_ID);

if ($video_ad_id)
{
	$result = q("select ad_owner,mv_package_id from $ads_tbl m inner join $AdExtraOptions m2 on m.ad_id=m2.mv_ad_id where ad_id = $video_ad_id");
	$row = mysql_fetch_array($result);
	$ad_owner = $row["ad_owner"];
	$packageId = $row["mv_package_id"];
	
	if ($ad_owner <> $_SESSION["valid_user"] AND !$admin_area)
		criticalError(LA_ERROR,LA_MUSTBEOWNER);
}
$tplUpload_video=new TplLoad;
if (isset($_REQUEST["video_ad_id"]))
{
	$video_ad_id = round($_REQUEST["video_ad_id"]);
	$tplUpload_video->assign("video_ad_id",$video_ad_id);
}
$tplUpload_video->assign("url", "http://$set_url");
$tplUpload_video->assign("phpself",$_SERVER['PHP_SELF']);

if ($set_payments)
{
	$lUser=$_SESSION["valid_user"];
	if ($admin_area)
	{
		$res=q("select ad_owner,mv_package_id from $ads_tbl m inner join $AdExtraOptions m2 on m.ad_id=m2.mv_ad_id where ad_id=" . $video_ad_id);	
		$row_user=mysql_fetch_array($res);
		$lUser=$row_user["ad_owner"];
		$packageId = $row_user["mv_package_id"];
	}
		
	if($packageId==0){
		$sql="select pay_upload_video from mvial_user_credits where user_id=$lUser and mainType=".$mainType." and package_id=".$packageId;
	}else{
		$sql="select b.pmatch,a.pay_upload_video from mvial_user_credits a inner join mvial_packages b on b.pid=a.package_id where a.user_id=$lUser and a.mainType=$mainType and a.package_id=$packageId";
	}
	$r_user=q($sql);
	$row_user=mysql_fetch_array($r_user);
	$lPayUpload_video=$row_user["pay_upload_video"];
	if($packageId==0){
		$lCountUploadVideoxPackage=$lPayUpload_video;
	}else{
		$lCountUploadVideoxPackage=$row_user["pmatch"];
	}
	if(empty($lPayUpload_video)) $lPayUpload_video=0;	
}
$size = floor($set_video_limit*1024);

if (isset($_REQUEST["submit"]))
{
	$lNumVideoUploaded=getNumberOfVideosUploaded($video_ad_id);
	if($lPayUpload_video==0){
		//$tplUpload_video->assign("no_credits", LA_UPLOAD_VIDEO_NO_CREDITS);
		$stop=1;
	}else if ($lNumVideoUploaded>=$lCountUploadVideoxPackage){
		$tplUpload_video->assign("upload_limit_max", LA_UPLOAD_VIDEO_LIMIT_MAX);
		$stop=1;	
	}
}

if (getParam("del_id",""))
{
	deleteVideo(getParamInt("del_id",""));
	$tplUpload_video->assign("show_form",1);	
	q("update $ads_tbl set ad_has_video=ad_has_video-1 where ad_id=$video_ad_id AND ad_has_video>0");
	
}

if (isset($_REQUEST["submit"]) && $stop!=1)
{
	$FileArray=$_FILES["photo"];
	$SaveAs=$set_path . "/images/videos/$video_ad_id";
	$MaxSize="$piclimit";
	$ad_id=getParamInt("videoad_id","");
	$returnValue=uploadImage($FileArray,$SaveAs,$set_video_limit,$DimensionArray,1,"");
	
	if (is_int($returnValue))
		$tplUpload_video->assign("error",getErrorMsgVideo($returnValue));
	else 
	{
		foreach ($returnValue as $item)
		{
			//echo "Filen $item ble lastet opp!<br>";
			//echo "<img src='images/$item'><br>";
			@chmod("images/videos/$item",octdec($set_chmod));
		}
		
		$sq = "INSERT INTO $vid_tbl (video_adid,video_file) VALUES ('$video_ad_id','$item')";
		$result=q($sq);
		$id = mysql_insert_id();
		
		$sq = "update $ads_tbl set ad_has_video = 0 where ad_id = $video_ad_id AND ad_has_video IS NULL";
		$re = q($sq);
			
		$sq = "update $ads_tbl set ad_has_video = ad_has_video + 1 where ad_id = $video_ad_id";
		$re = q($sq);
		
		$tplUpload_video->assign("upload_ok",1);
		//Bajamos los creditos si estos son de obsequio
		if($packageId == 0){
			$sq = "update mvial_user_credits SET pay_upload_video=pay_upload_video-1 where user_id=$lUser and mainType=".$mainType." and package_id=".$packageId;
			$re = q($sq);			
		}
		
	}
}

$sq = "select video_id from $vid_tbl where video_adid = " . $video_ad_id;
$result2 = q($sq);
$mysql_img = mysql_num_rows($result2);

if ($mysql_img==0){ 
$mysql_img = 0;} 

$tplUpload_video->assign("image_upload_size",formatString(html_entity_decode(LA_UPLOAD_VIDEO_LIMIT), array($set_video_limit)));

if ($set_payments)
	$set_video_num=$lPayUpload_video;
if(!isset($lNumVideoUploaded))
	$lNumVideoUploaded=getNumberOfVideosUploaded($video_ad_id);
if ($lNumVideoUploaded>=$lCountUploadVideoxPackage){
	$tplUpload_video->assign("upload_limit_max", LA_UPLOAD_VIDEO_LIMIT_MAX);
	$stop=1;	
}else if ($mysql_img>=$set_video_num){
	$stop=1;
	$tplUpload_video->assign("no_credits", LA_UPLOAD_VIDEO_NO_CREDITS);	
}

	
$tplUpload_video->assign("upload_status", formatString(LA_UPLOAD_VIDEO_STATUS,array("$mysql_img","$lCountUploadVideoxPackage")));


// If less than maxpic has been uploaded, show upload-form
if (!$stop)
{
	$tplUpload_video->assign("show_form",1);
}

if ($video_ad_id)
{
	$query = "select * from $vid_tbl where video_adid=$video_ad_id order by video_id desc";
	$sql_result = q ($query);
	$num_pictures =  mysql_num_rows($sql_result);
	
	for ($i=0; $i<$num_pictures; $i++)
	{
	    $row = mysql_fetch_array($sql_result);  
		$id = $row["video_id"];
		$filetype = $row["filetype"];
		$filename = $row["video_file"];
		
		$fArray[$i]["id"] = $id;
		$fArray[$i]["name"] = $filename;		
		
	}
	$tplUpload_video->assign("image_list", $fArray);
}

$tplUpload_video->assign("id", $video_ad_id);
$tplUpload_video->display("upload_video.tpl");
if (!$admin_area)
{
	include_once("member_footer.php");
	include_once("footer_inc.php"); 
}

?>
